﻿/*!
* jQuery DivMenu plugin: 
* 
* version 0.1 2010-03-08
* Requires jQuery v1.3.2 or later
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Authors: Kim Seoung Min
*/
; (function($) {
    if (!$.KDBJQ) {
        $.KDBJQ = new Object();
    };

    $.KDBJQ.supercontextMenu = function(el, boardmenu) {

        var base = this;
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("KDBJQ.supercontextMenu", base);

        base.init = function() {
            //  base.liname = liname;
            // Put your initialization code here
        };

        // Run initializer
        base.init();

        base.$el.bind("mousedown", function(e) {
            if (e.which == 1) {
                if ($(document.body).find('> .DivContextMenu').html() != null) {
                    $(document.body).find('> .DivContextMenu').remove()
                }

                $(document.body).append("<div class='DivContextMenu'></div>");

                var menutop = base.$el.offset().top + base.$el.outerHeight(); //base.$el.offset().top + base.$el.outerHeight();
                var menuleft = base.$el.offset().left ; // base.$el.offset().left;

                $(document.body).find('> .DivContextMenu').css("top", menutop);
                $(document.body).find('> .DivContextMenu').css("left", menuleft);

                for (var i = 0; i < boardmenu.Menu.length; i++) {
                    var _name = boardmenu.Menu[i].name;
                    var _img = boardmenu.Menu[i].img;
                    var _cmd = boardmenu.Menu[i].cmd;

                    var _str = String.format("<li class='edit' cmd='{2}'><img src='{1}' /><a>{0}</a></li>", _name, _img, _cmd);
                    $(document.body).find('> .DivContextMenu').append(_str);
                }

                $(document.body).find('> .DivContextMenu li').bind("click",{obj: base.$el}, function(e) {
                    $(document.body).find('> .DivContextMenu').remove();
                    boardmenu.Click(e, this);
                });
                

                $(document.body).find('> .DivContextMenu').bind("mouseleave", function(e) {
                    $(document.body).find('> .DivContextMenu').remove();
                });

                $(document.body).find('> .DivContextMenu').bind("mouseover", function(e) {
                   $(document.body).find('> .DivContextMenu').data("MouseOver", true);
                });
                $(document.body).find('> .DivContextMenu').data("MouseOver", false);

                $(document.body).find('> .DivContextMenu').delay(2000).queue(function() {
                    if ($(document.body).find('> .DivContextMenu').data("MouseOver") == false)
                    $(document.body).find('> .DivContextMenu').remove();
                });


                return false;    
                
            }

            return true;
           
        });


        //        base.$el.bind("mouseover", function(e) {
        //            if ($(document.body).find('> .DivContextMenu').html() != null) {
        //                $(document.body).find('> .DivContextMenu').remove()
        //            }
        //            $(document.body).append("<div class='DivContextMenu'></div>");
        //            //  $(document.body).find('> .DivContextMenu').css("top", e.clientY)

        //            var menutop = base.$el.offset().top + base.$el.outerHeight();
        //            var menuleft = base.$el.offset().left;

        //            $(document.body).find('> .DivContextMenu').css("top", menutop);
        //            $(document.body).find('> .DivContextMenu').css("left", menuleft);
        //            //alert('offset_top : ' + base.$el.offset().top + ' - ' + base.$el.outerHeight());
        //            //alert('offsety : ' + base.$el.offsety);
        //            base.$menu = $("#" + base.liname);
        //            $(document.body).find('> .DivContextMenu').append(base.$menu.html());
        //            $(document.body).find('> .DivContextMenu').bind("mouseleave", function(e) {
        //                $(document.body).find('> .DivContextMenu').remove()
        //            });
        //        });

    };




    $.fn.supercontextMenu = function(liname) {
        return this.each(function() {
            (new $.KDBJQ.supercontextMenu(this, liname));
        });
    };


})(jQuery);